package com.li.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.li.constant.MessageConstant;
import com.li.entity.PageResult;
import com.li.entity.QueryPageBean;
import com.li.entity.Result;
import com.li.mapper.MemberMapper;
import com.li.mapper.OrderMapper;
import com.li.mapper.OrderSettingMapper;
import com.li.pojo.Member;
import com.li.pojo.Order;
import com.li.pojo.OrderSetting;
import com.li.service.OrderService;
import com.li.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    OrderMapper orderMapper;

    //进行预约
    public Result order(Map map) throws Exception {

        //当前日期是否可预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(date);
        if (orderSetting == null) {
            //不可预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //是否预约满
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            //预约已满
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //是否是会员,不是会员将自动注册会员
        String telephone = (String) map.get("telephone");
        Member member = memberMapper.findByTelephone(telephone);
        if (member != null) {
            //是会员 查看是否重复预约 会员id 日期 套餐id
            Integer memberId = member.getId();
            String setmealId = (String) map.get("setmealId");
            Order order = new Order(memberId, date, Integer.parseInt(setmealId));
            List<Order> list = orderMapper.findByCondition(order);
            if (list != null && list.size() > 0) {
                //已经预约 不能重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }

        //可以预约  预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingMapper.editReservationsByOrderDate(orderSetting);

        if (member == null) {
            //不是会员 自动注册会员
            member = new Member();

            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setSex((String) map.get("sex"));
            member.setIdCard((String) map.get("idCard"));
            member.setRegTime(DateUtils.parseDate2String(new Date()));

            memberMapper.add(member);
        }
        //开始预约
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(date);
        order.setOrderType((String) map.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderMapper.add(order);

        //插入预约设置
        orderMapper.add(order);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map findById(Integer id) {

        Map map = orderMapper.findById(id);
        if (map != null) {
            Date order_date = (Date) map.get("order_date");

            try {
                map.put("order_date", DateUtils.parseDate2String(order_date));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return map;
    }

    @Override
    public PageResult findOrders(QueryPageBean queryPageBean) {
        //设置分页
        //PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 查询 数据库
        List<Map> page = orderMapper.findOrders(queryPageBean.getQueryString());
        System.out.println(page);
        return new PageResult((long) page.size(), page);
    }

    @Override
    public void deleteOrderById(Integer id) {
        orderMapper.deleteOrderById(id);
    }
}
